package com.dzjcloud.dzj.upms.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzjcloud.dzj.common.redis.constant.RedisInfoConstant;
import com.dzjcloud.dzj.common.redis.utils.MsgSendUtils;
import com.dzjcloud.dzj.common.redis.utils.RedisUtil;
import com.dzjcloud.dzj.upms.api.entity.Gateway;
import com.dzjcloud.dzj.upms.biz.mapper.GatewayMapper;
import com.dzjcloud.dzj.upms.biz.service.GatewayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 基础平台网关路由规则表(Gateway)表服务实现类
 *
 * @author makejava
 * @since 2023-04-10 09:25:41
 */
@Service
@Slf4j
public class GatewayServiceImpl extends ServiceImpl<GatewayMapper, Gateway> implements GatewayService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Gateway> reloadRouter() {
       //TODO 1查询未删除并开启的路由
        List<Gateway> gateways = this.list(Wrappers.<Gateway>lambdaQuery().eq(Gateway::getEnableStatus, 1));
        log.info("------------------->路由信息：{}",gateways);

        //TODO 写进redis
        redisUtil.set(RedisInfoConstant.GATEWAY_ROUTER_INFO_KEY,  JSONObject.toJSONString(gateways));

        // 通知网关刷新
        MsgSendUtils.sendMsg(stringRedisTemplate, RedisInfoConstant.ROUTER_RELOAD_REDIS_SUBSCRIBE_KEY,  JSONObject.toJSONString(gateways));
        return gateways;
    }
}

